package com.imy.common.util;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Process;
import android.provider.Settings;

import androidx.annotation.NonNull;

import java.lang.reflect.Method;
import java.util.List;

public class AppUtils {

    /**
     * 返回当前的进程名
     */
    public   static  String getProcessName(@NonNull Context context) {
        int pid = Process.myPid();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningAppProcessInfo> runningAppList = am.getRunningAppProcesses();
            if (runningAppList != null) {
                for (ActivityManager.RunningAppProcessInfo processInfo : runningAppList) {
                    if (processInfo.pid == pid) {
                        return processInfo.processName;
                    }
                }
            }
        }
        return null;
    }

    public static List<PackageInfo> getSystemInstallPackage(Context context) {
        PackageManager packageManager = context.getPackageManager();
        return packageManager.getInstalledPackages(0);
    }
    public static int openApp(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        Intent launchIntentForPackage = packageManager.getLaunchIntentForPackage(packageName);
        if (launchIntentForPackage!=null){
            context.startActivity(launchIntentForPackage);
            return 0;
        }else {
            return -1;
        }

    }
    public static PackageInfo isSystemInstallPackage( Context context,String packageName) {
        List<PackageInfo> systemInstallPackage = getSystemInstallPackage(context);
        for (int i = 0; i < systemInstallPackage.size(); i++) {
            String packageName1 = systemInstallPackage.get(i).packageName;
            if (packageName1.equals(packageName)) {
                return systemInstallPackage.get(i);
            }
        }
        return null;
    }

    public static String getPackageNameByName(Context context,String checkjstring) {
        List<PackageInfo> systemInstallPackage = getSystemInstallPackage(context);
        for (int i = 0; i < systemInstallPackage.size(); i++) {
            ApplicationInfo applicationInfo = systemInstallPackage.get(i).applicationInfo;
            String charSequence = (String) applicationInfo.loadLabel(context
                    .getPackageManager()).toString();
            if (charSequence.equals(checkjstring)){
                return systemInstallPackage.get(i).packageName;
            }
        }
        return "";
    }


    //版本名
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    //版本号
    public static String getVersionCode(Context context) {
        return String.valueOf(getPackageInfo(context).versionCode);
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }


}
